<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ include file="/WEB-INF/jsp/commons/global.jsp"%>

<table id="categoryVideoList" style="height: 100%; width: 100%;">

</table>

<div id="categoryVideoListToolBars" style="height: auto">
	<a href="javascript:void(0)" class="easyui-linkbutton"
		data-options="iconCls:'icon-remove',plain:true"
		onclick="categoryVideoList_remove()">删除</a>| <a
		href="javascript:void(0)" class="easyui-linkbutton"
		data-options="iconCls:'icon-add',plain:true"
		onclick="categoryVideoList_append()">添加</a>|<a
		href="javascript:void(0)" class="easyui-linkbutton"
		data-options="iconCls:'icon-edit',plain:true"
		onclick="categoryVideoList_edit()">编辑</a> | <a
		href="javascript:void(0)" class="easyui-linkbutton"
		data-options="iconCls:'icon-undo',plain:true"
		onclick="categoryVideoList_undo()">取消</a>| <a
		href="javascript:void(0)" class="easyui-linkbutton"
		data-options="iconCls:'icon-save',plain:true"
		onclick="categoryVideoList_accept()">保存</a> |<input
		id="categoryVideoList_VideoStatusQue" style="width: 100px;" /> |<input
		id="categoryVideoList_videoTitleQue" style="width: 200px;" /> | <a
		href="javascript:void(0)" class="easyui-linkbutton"
		data-options="iconCls:'fa fa-cog',plain:true"
		onclick="categoryVideoList_categoryListPage()">设置分类</a>
</div>


<script type="text/javascript">
	function categoryVideoListDatagridReload() {
		try {
			var queryParams = {
				like_title : categoryVideoList_videoTitleQue
						.textbox('getValue'),
				itemType : 1,
				status : categoryVideoList_VideoStatusQue.combobox('getValue')
			};
			// 			,
			// 			status : videoList_StatusQue.combobox('getValue')
			categoryVideoListDatagrid.datagrid("options").queryParams = queryParams;
			categoryVideoListDatagrid.datagrid("reload");
		} catch (e) {
		}
	}
</script>

<script type="text/javascript">
	var categoryVideoList_VideoStatusQue = undefined;
	var categoryVideoList_videoTitleQue = undefined;
	var categoryVideoSelectIndex = undefined;
	var categoryVideoSelectRow = undefined;
	var categoryVideoEditIndex = undefined;
	var categoryVideoEditRow = undefined;
	var categoryVideoListDatagrid = undefined;
	$(function() {
		categoryVideoList_VideoStatusQue = $(
				'#categoryVideoList_VideoStatusQue').combobox({
			data : [ {
				"statusCode" : 0,
				"statusMsg" : "全部状态",
				"selected" : true
			}, {
				"statusCode" : 1,
				"statusMsg" : "已发布"
			}, {
				"statusCode" : 2,
				"statusMsg" : "已下线"
			}, {
				"statusCode" : 3,
				"statusMsg" : "待发布"
			} ],
			valueField : 'statusCode',
			textField : 'statusMsg',
			onChange : function(newValue, oldValue) {
				categoryVideoListDatagridReload();
			}
		});

		categoryVideoList_videoTitleQue = $('#categoryVideoList_videoTitleQue')
				.textbox({
					buttonText : '标题搜索',
					buttonIcon : 'icon-search',
					buttonAlign : 'right'
				});

		categoryVideoList_videoTitleQue.textbox('button').click(function() {
			categoryVideoListDatagridReload();
		});

		categoryVideoListDatagrid = $('#categoryVideoList')
				.datagrid(
						{
							url : '${baseUrl}/category/item/list',
							pagination : true,
							sortName : 'sort,updateTime',
							sortOrder : 'desc,desc',
							multiSort : true,
							//remoteSort : false,
							// 			rownumbers : true,
							queryParams : {
								itemType : 1
							},
							singleSelect : true,
							idField : 'id',
							striped : true,
							pageSize : 20,
							fitColumns : true,
							toolbar : '#categoryVideoListToolBars',
							onLoadSuccess : function() {
								categoryVideoSelectIndex = undefined;
								categoryVideoSelectRow = undefined;
								categoryVideoEditIndex = undefined;
								categoryVideoEditRow = undefined;
							},
							onBeforeSelect : function(index, row) {
								if (categoryVideoEditIndex != undefined) {
									return false;
								} else {
									categoryVideoSelectIndex = index;
									categoryVideoSelectRow = row;
								}
							},
							onBeforeEdit : function(index, row) {
								if (categoryVideoEditIndex == undefined) {
									categoryVideoEditIndex = index;
									categoryVideoEditRow = row;
								}
							},
							onEndEdit : function(index, row, changes) {
								var _ed = $(this).datagrid('getEditor', {
									index : index,
									field : 'statusCode'
								});
								row.statusMsg = $(_ed.target).combobox(
										'getText');
							},
							// 			nowrap : false,
							columns : [ [
									{
										field : 'cover_url',
										title : '视频封面',
										width : 100,
										align : 'center',
										formatter : function(value, row, index) {
											if (row.item) {
												var img = '<img src="'
														+ row.item.coverUrl
														+ '" style="width:103%;height:100px;"/>';
												return img;
											} else {
												return value;
											}
										}
									},
									{
										field : 'id',
										title : 'ID',
										align : 'center',
										width : 60
									},
									{
										field : 'title',
										title : '视频标题',
										width : 249,
										formatter : function(value, row, index) {
											if (row.item) {
												return row.item.title;
											} else {
												return value;
											}
										}
									},
									{
										field : 'youshengshuCategory',
										title : '有声书',
										align : 'center',
										width : 60
									},
									{
										field : 'yinyueCategory',
										title : '音乐',
										align : 'center',
										width : 60
									},
									//TODO 可以继续增加 资源类别
									{
										field : 'statusCode',
										title : '状态',
										width : 80,
										align : 'center',
										formatter : function(value, row, index) {
											var statusCodeSpan = '<span style="';
											if (row.statusCode == 1) {
												statusCodeSpan += 'color:#6B8E23;font-size:13px;font-weight:600;';
											} else if (row.statusCode == 2) {
												statusCodeSpan += 'color:#8B4726;font-size:13px;font-weight:600;';
											} else if (row.statusCode == 3) {
												statusCodeSpan += 'color:#104E8B;font-size:13px;font-weight:600;';
											}
											return statusCodeSpan += '">'
													+ row.statusMsg + '</span>';
										},
										editor : {
											type : 'combobox',
											options : {
												valueField : 'statusCode',
												textField : 'statusMsg',
												data : [ {
													statusCode : 1,
													statusMsg : '已发布'
												}, {
													statusCode : 2,
													statusMsg : '已下线'
												}, {
													statusCode : 3,
													statusMsg : '待发布'
												} ],
												required : true
											}
										}
									},
									{
										field : 'sort',
										title : '排序',
										width : 120,
										align : 'center',
										sortable : true,
										editor : {
											type : 'numberbox',
											options : {
												min : 0,
												precision : 2,
												required : true
											}
										}
									},
									{
										field : 'hotSort',
										title : '热门排序',
										width : 180,
										align : 'center',
										sortable : true,
										editor : {
											type : 'numberbox',
											options : {
												min : 0,
												precision : 2,
												required : true
											}
										}
									},
									{
										field : 'createTime',
										title : '创建时间',
										width : 100,
										align : 'left'
									},
									{
										field : 'updateTime',
										title : '更新时间',
										width : 100,
										align : 'left',
										sortable : true
									},
									{
										field : 'createUser',
										title : '创建者',
										width : 60,
										align : 'center',
										formatter : function(value, row, index) {
											if (row.createUser) {
												return row.createUser.nickname;
											} else {
												return value;
											}
										}
									},
									{
										field : 'itemId',
										title : '视频ID',
										width : 80,
										align : 'center',
										formatter : function(value, row, index) {
											if (row.item) {
												return row.item.id;
											} else {
												return value;
											}
										}
									},
									{
										field : 'entity',
										title : '视频实体',
										width : 80,
										align : 'center',
										formatter : function(value, row, index) {
											if (row.item) {
												return row.item.entity;
											} else {
												return value;
											}
										}
									},
									{
										field : 'parent',
										title : '所属视频集合',
										width : 200,
										align : 'left',
										formatter : function(value, row, index) {
											if (row.item.parent) {
												return "<span>集合ID:"
														+ row.item.parent.id
														+ "</span><br/><span>集合标题:"
														+ row.item.parent.title
														+ "</span>";
											} else {
												return value;
											}
										}
									},
									{
										field : 'summary',
										title : '简介',
										width : 70,
										hidden : true,
										align : 'left',
										formatter : function(value, row, index) {
											if (row.item) {
												return row.item.summary;
											} else {
												return value;
											}
										}
									},
									{
										field : 'info',
										title : '详细介绍',
										hidden : true,
										width : 70,
										align : 'left',
										formatter : function(value, row, index) {
											if (row.item) {
												return row.item.info;
											} else {
												return value;
											}
										}
									},
									{
										field : 'playtime',
										title : '播放时间(秒)',
										width : 100,
										align : 'center',
										formatter : function(value, row, index) {
											if (row.item) {
												return row.item.playtime;
											} else {
												return value;
											}
										}
									},
									{
										field : 'itemStatusCode',
										title : '视频状态',
										width : 100,
										align : 'center',
										formatter : function(value, row, index) {
											var statusCodeSpan = '<span style="';
											if (row.item.statusCode == 1) {
												statusCodeSpan += 'color:#6B8E23;font-size:13px;font-weight:600;';
											} else if (row.item.statusCode == 2) {
												statusCodeSpan += 'color:#8B4726;font-size:13px;font-weight:600;';
											} else if (row.item.statusCode == 3) {
												statusCodeSpan += 'color:#104E8B;font-size:13px;font-weight:600;';
											}
											return statusCodeSpan += '">'
													+ row.item.statusMsg
													+ '</span>';
										}
									},
									{
										field : 'progressStatusCode',
										title : '视频进度',
										width : 100,
										align : 'center',
										formatter : function(value, row, index) {
											var statusCodeSpan = '<span style="';
											if (row.progressStatusCode == 2) {
												statusCodeSpan += 'color:#6B8E23;font-size:13px;font-weight:600;';
											} else if (row.progressStatusCode == 1) {
												statusCodeSpan += 'color:#FFEC8B;font-size:13px;font-weight:600;';
											}
											return statusCodeSpan += '">'
													+ row.progressStatusMsg
													+ '</span>';
										}
									} ] ],
							onDblClickRow : function(index, row) {
								$.ajax({
									data : {
										id : row.item.id
									},
									async : false,
									url : baseUrl + '/video/info.html',
									success : function(html) {
										$('body').append(html);
									}
								});

							}
						});

		categoryVideoListDatagrid.datagrid('getPager').pagination(
				{
					buttons : [ {
						iconCls : 'fa fa-2x fa-location-arrow',
						handler : function() {
							categoryVideoListDatagrid.datagrid('gotoPage',
									categoryVideoListDatagrid.datagrid(
											'getPager').find('.pagination-num')
											.val());
						}
					} ]
				});

	});

	function categoryVideoList_append() {

		$.ajax({
			url : baseUrl + '/category/item/add.html?itemType=1',
			success : function(html) {
				$('body').append(html);
			}
		});
	}
	function categoryVideoList_edit() {

		var categoryVideoSelect = categoryVideoListDatagrid
				.datagrid('getSelected');
		if (categoryVideoEditIndex != undefined
				&& categoryVideoEditIndex != categoryVideoSelectIndex) {
			$.messager.alert('提示', '请完成已开启编辑行', 'warning');
			return;
		}

		if (categoryVideoSelect) {
			if (categoryVideoSelectIndex == undefined) {
				categoryVideoSelectIndex = categoryVideoListDatagrid.datagrid(
						"getRowIndex", categoryVideoSelect);
			}

			categoryVideoListDatagrid.datagrid('beginEdit',
					categoryVideoSelectIndex);
			categoryVideoSelectRow = categoryVideoSelect;
		}
	}

	function categoryVideoList_undo() {
		if (categoryVideoSelectIndex != undefined) {
			categoryVideoListDatagrid.datagrid('rejectChanges');
			categoryVideoSelectIndex = undefined;
			categoryVideoSelectRow = undefined;
			categoryVideoEditIndex = undefined;
			categoryVideoEditRow = undefined;
		}
	}

	function categoryVideoList_accept() {
		if (categoryVideoSelectIndex != undefined) {
			if (categoryVideoListDatagrid.datagrid('validateRow',
					categoryVideoSelectIndex)) {

				//验证发布状态
				var _edStatusCode = categoryVideoListDatagrid.datagrid(
						'getEditor', {
							index : categoryVideoSelectIndex,
							field : 'statusCode'
						});
				var _statusMsg = $(_edStatusCode.target).combobox('getText');
				if (_statusMsg != '已发布' && _statusMsg != '待发布'
						&& _statusMsg != '已下线') {
					$.messager.alert('提示', '未知状态', 'warning');
					return;
				}

				//获取排序
				var _edSort = categoryVideoListDatagrid.datagrid('getEditor', {
					index : categoryVideoSelectIndex,
					field : 'sort'
				});

				var _Sort = $(_edSort.target).val();

				//获取热门排序
				var _edHotSort = categoryVideoListDatagrid.datagrid(
						'getEditor', {
							index : categoryVideoSelectIndex,
							field : 'hotSort'
						});

				var _hotSort = $(_edHotSort.target).val();

				var _Data = {
					itemId : categoryVideoSelectRow.item.id,
					itemType : 1,
					sort : _Sort,
					hotSort : _hotSort,
					status : $(_edStatusCode.target).combobox('getValue')
				};

				$
						.ajax({
							data : _Data,
							async : false,
							type : 'POST',
							url : baseUrl + '/category/item/update',
							success : function(data) {
								if (data.responseCode == '000') {
									categoryVideoListDatagrid
											.datagrid('endEdit',
													categoryVideoSelectIndex);

									categoryVideoListDatagrid
											.datagrid('acceptChanges');

									categoryVideoSelectIndex = undefined;
									categoryVideoSelectRow = undefined;
									categoryVideoEditIndex = undefined;
									categoryVideoEditRow = undefined;

									$.messager.alert('提示', '修改成功', 'info');
								} else {
									$.messager.alert('提示',
											data.responseMessage, 'error');
								}
							}
						});

			}
		}
	}

	function categoryVideoList_categoryListPage() {
		var categoryVideoSelect = categoryVideoListDatagrid
				.datagrid('getSelected');
		if (categoryVideoSelect) {
			$.ajax({
				data : {
					itemId : categoryVideoSelect.item.id,
					itemType : 1
				},
				async : false,
				url : baseUrl + '/category/set.html',
				success : function(html) {
					$('body').append(html);
				}
			});
		}
	}

	function categoryVideoList_remove() {
		var categoryVideoSelect = categoryVideoListDatagrid
				.datagrid('getSelected');
		if (categoryVideoSelect) {
			$.messager.confirm('确认', '您确认想要删除吗?', function(r) {
				if (r) {
					$.ajax({
						data : {
							rid : categoryVideoSelect.id
						},
						async : false,
						url : baseUrl + '/category/item/delete',
						success : function(data) {
							if (data.responseCode == '000') {
								$.messager.alert('提示', "删除成功!", 'info');

								categoryVideoListDatagrid.datagrid('deleteRow',
										categoryVideoListDatagrid.datagrid(
												'getRowIndex',
												categoryVideoSelect));

							} else {
								$.messager.alert('提示', data.responseMessage,
										'error');
							}
						}
					});
				}
			});
		}
	}
</script>
